from .ofc2 import *
import time, random, math

def ifrange(start, end=None, step=1.0):
    if end is None:
        end = start
        start = 0.0
    while start<=end:
        yield start
        start += step

def frange(start, end=None, step=1.0):
    return list(ifrange(start, end, step))

def scurve():
    return [math.sin(i)*2 for i in ifrange(0, 6.2, 0.2)]

def scircle():
    return [dict(x=math.sin(i), y=math.cos(i)) for i in ifrange(0, 6.2, 0.1)]
# ____________________________________________________________________________ #

def test_area_hollow():

    t = title(text="Area Hollow Chart")
    a = area_hollow()
    a.width = 2
    a.dot_size = 4
    a.halo_size = 1
    a.colour = '#C4B86A'
    a.fill_colour = '#C4B8AA'
    a.fill_alpha = 0.7
    a.values = scurve()
    chart = open_flash_chart()
    chart.title = t
    chart.add_element(a)
    y  = y_axis()
    y.min, y.max, y.steps = -2, 2, 2
    y.offset = False

    x = x_axis()
    x.steps = 2
    xlbls = x_axis_labels(steps=2, rotate=270, visible_steps=1)
    x.labels = xlbls

    chart.y_axis = y
    chart.x_axis = x
    return chart

def test_area_line():

    t = title(text="Area Chart")
    a = area_line()
    a.width = 2
    a.dot_size = 4
    a.halo_size = 1
    a.colour = '#C4B86A'
    a.fill_colour = '#C4B8AA'
    a.fill_alpha = 0.7
    a.values = scurve()
    chart = open_flash_chart()
    chart.title = t
    chart.add_element(a)
    y  = y_axis()
    y.min, y.max, y.steps = -2, 2, 2
    y.offset = False

    x = x_axis()
    x.steps = 2
    xlbls = x_axis_labels(steps=2, rotate=270, visible_steps=1)
    x.labels = xlbls

    chart.y_axis = y
    chart.x_axis = x
    return chart

def test_bar():

    t = title(text=time.strftime('%a %Y %b %d'))
    b = bar()
    b.values = list(range(9,0,-1))
    chart = open_flash_chart()
    chart.title = t
    chart.add_element(b)
    return chart

def test_bar_2():

    t = title(text=time.strftime('%a %Y %b %d'))
    b1 = bar()
    b1.values = list(range(9,0,-1))
    b2 = bar()
    b2.values = [random.randint(0,9) for i in range(9)]
    b2.colour = '#56acde'
    chart = open_flash_chart()
    chart.title = t
    chart.add_element(b1)
    chart.add_element(b2)
    return chart
    
def test_bar_filled():

    t = title(text=time.strftime('%a %Y %b %d'))
    b1 = bar_filled(colour='#E2D66A')
    b1.values = list(range(9,0,-1))
    b1.outline_colour = '#577261'
    chart = open_flash_chart(bg_colour="#FFFFFF")
    chart.title = t
    chart.add_element(b1)
    return chart

def test_horizontal_bar():
    months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    t = title(text="Our New House Schedule")
    hb = hbar(colour="#86BBEF")
    hb.tooltip = ("Months: #val#")
    values = [ hbar_value(left=0, right=4),
        hbar_value(left=4, right=8)]
    h3 = hbar_value(left=8, right=11)
    h3.tip = "#left# to #right#<br>%s to %s (#val# months)" % (months[8], months[11])
    values.append(h3)
    hb.values = values
    chart = open_flash_chart()
    chart.title = t
    chart.add_element(hb)
    x = x_axis()
    x.offset = False
    x.labels = x_axis_labels(labels=months)
    y = y_axis()
    y.offset = 1
    y.labels = ["Make garden look sexy","Paint house","Move into house"]
    chart.x_axis = x
    chart.y_axis = y
    return chart

def test_line():

    t = title(text=time.strftime('%a %Y %b %d'))
    l = line()
    l.values = [9,8,7,6,5,4,3,2,1]
    chart = open_flash_chart()
    chart.title = t
    chart.add_element(l)
    return chart

def test_line_dot():

    t = title(text=time.strftime('%a %Y %b %d'))
    l1 = line_dot()
    l1.values = list([math.sin(x)*1.9 + 7 for x in frange(0,6.2, 0.2)])
    l1.halo_size = 0
    l1.width = 2
    l1.dot_size = 4

    l2 = line_dot()
    l2.values = list([math.sin(x)*1.9 + 10 for x in frange(0,6.2, 0.2)])
    l2.halo_size = 1
    l2.width = 1
    l2.dot_size = 4

    l3 = line_dot()
    l3.values = list([math.sin(x)*1.9 + 4 for x in frange(0,6.2, 0.2)])
    l3.halo_size = 1
    l3.width = 6
    l3.dot_size = 4

    y = y_axis()
    y.min, y.max, y.steps = 0, 15, 5

    chart = open_flash_chart()
    chart.title = t
    chart.add_element(l1)
    chart.add_element(l2)
    chart.add_element(l3)
    chart.y_axis = y
    return chart

def test_line_hollow():

    t = title(text=time.strftime('%a %Y %b %d'))
    l1 = line_hollow()
    l1.values = list([math.sin(x)*1.9 + 7 for x in frange(0,6.2, 0.2)])
    l1.halo_size = 0
    l1.width = 2
    l1.dot_size = 4

    l2 = line_hollow()
    l2.values = list([math.sin(x)*1.9 + 10 for x in frange(0,6.2, 0.2)])
    l2.halo_size = 1
    l2.width = 1
    l2.dot_size = 4

    l3 = line_hollow()
    l3.values = list([math.sin(x)*1.9 + 4 for x in frange(0,6.2, 0.2)])
    l3.halo_size = 1
    l3.width = 6
    l3.dot_size = 4

    y = y_axis()
    y.min, y.max, y.steps = 0, 15, 5

    chart = open_flash_chart()
    chart.title = t
    chart.add_element(l1)
    chart.add_element(l2)
    chart.add_element(l3)
    chart.y_axis = y
    return chart

def test_manylines():

    l1 = line_dot()
    l1.width = 4
    l1.colour = "#DFC329"
    l1.dot_size = 5
    l1.values = [random.randint(1,6) for i in  range(9)]
    l1.text = 'Line 1'
    l1.font_size = 10

    l2 = line_hollow()
    l2.width = 1
    l2.colour = "#6363ac"
    l2.dot_size = 5
    l2.values = [random.randint(7,13) for i in  range(9)]
    l2.text = 'Line 2'
    l2.font_size = 10

    l3 = line()
    l3.width = 4;
    l3.colour = "#5E4725"
    l3.values = [random.randint(14,19) for i in  range(9)]
    l3.text = 'Line 3'
    l3.font_size = 10

    y = y_axis()
    y.min, y.max, y.steps = 0, 20, 5

    t = title(text='Three lines example')
    chart = open_flash_chart()
    chart.title = t
    chart.add_element(l1)
    chart.add_element(l2)
    chart.add_element(l3)
    chart.y_axis = y
    return chart

def test_radar_charts_1():

    chart = open_flash_chart()
    chart.title = title(text='Radar Chart')
    area = area_hollow()
    area.width = 1
    area.dot_size = 1
    area.halo_size = 1
    area.colour = '#45909F'
    area.fill_colour = '#45909F'
    area.fill_alpha = 0.4
    area.loop = True
    area.values = [3, 4, 5, 4, 3, 3, 2.5]
    chart.add_element(area)
    r = radar_axis(max=5)
    r.colour ='#EFD1EF'
    r.grid_colour = '#EFD1EF'
    ra = radar_axis_labels(labels=['0','1','2','3','4','5'])
    ra.colour = '#9F819F'
    r.labels = ra
    chart.radar_axis = r
    tip = tooltip()
    tip.proximity = 1
    chart.tooltip = tip
    chart.bg_colour = '#DFFFEC'
    return chart

def test_radar_charts_2():

    chart = open_flash_chart()
    chart.title = title(text='Radar Chart')
    line1 = line_hollow()
    line1.width = 1
    line1.dot_size = 3
    line1.halo_size = 2
    line1.colour = '#FBB829'
    line1.values = [3, 4, 5, 4, 3, 3, 2.5]
    line1.tip = "Gold #val#"
    line1.text = 'Mr. Gold'
    line1.font_size = 10
    chart.add_element(line1)

    line2 = line_dot()
    line2.values = [2 for i in range(7)]
    line2.halo_size = 2
    line2.width = 1
    line2.dot_size = 3
    line2.colour = '#80000FF'
    line2.tip = 'Purple #val#'
    line2.text = 'Mr. Purple'
    line2.font_size = 10
    line2.loop = True
    chart.add_element(line2)

    r = radar_axis(max=5)
    r.colour ='#DAD5E0'
    r.grid_colour = '#DAD5E0'
    ra = radar_axis_labels(labels=['Zero','','','Middle','','High'])
    ra.colour = '#9F819F'
    r.labels = ra
    sa = radar_spoke_labels(labels=['Strength', 'Smarts', 'Sweet<br>Tooth',
            'Armour', 'Max Hit Points', '???', 'Looks Like a Monkey'])
    sa.colour = '#9F819F'
    chart.radar_axis = r
    r.spoke_labels = sa
    tip = tooltip()
    tip.proximity = 1
    chart.tooltip = tip
    chart.bg_colour = '#FFFFFF'
    return chart

def test_radar_charts_3():

    chart = open_flash_chart()
    chart.title = title(text='Radar Chart')

    val1 = [30,50,60,70,80,90,100,115,130,115,100,90,80,70,60,50]
    spokes = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p']
    val2 = []

    for i in val1:
        txt = "#val#<br>Spoke: %s" % i
        tmp = dot_value(value=i,  colour='#D41E47', tip=txt)
        val2.append(tmp)
    line = line_hollow()
    line.values = val2
    line.halo_size = 0
    line.width = 2
    line.dot_size = 6
    line.colour = '#FBB829'
    line.text = 'Hearts'
    line.font_size = 10
    line.loop = True
    chart.add_element(line)
    r = radar_axis(max=150)
    r.steps = 10
    r.colour = '#DAD5E0'
    r.grid_colour = '#EFEFEF'
    chart.radar_axis = r
    tip = tooltip(proximity=1)
    chart.tooltip = tip
    chart.bg_colour = '#FFFFFF'
    return chart

def test_scatter_chart():

    chart = open_flash_chart()
    chart.title = title(text='Scatter Chart')
    s1 = scatter(colour="#FFD600", dot_size=10)
    s1.values = [scatter_value(x=0, y=0)]
    chart.add_element(s1)
    s2 = scatter(colour="#D600FF", dot_size=3)
    s2.values = scircle()
    chart.add_element(s2)
    x = x_axis()
    y = y_axis()
    x.min, x.max, x.steps = -2, 2, 1
    y.min, y.max, y.steps = -2, 2, 1
    chart.x_axis = x
    chart.y_axis = y
    return chart

def test_scatter_line_chart():
    chart = open_flash_chart()
    chart.title = title(text='Scatter Line Chart')
    s = scatter_line(colour="#FFD600", dot_size=3)
    x = 0.0
    y = 0
    v = []
    while(x<25):
        v.append(scatter_value(x=x,y=y))
        y = random.randint(-20,20)/10
        if y > 10: y = 10
        if y < -10 : y = -10
        x += random.randint(5, 15)/10
    s.values = v
    chart.add_element(s)

    xa = x_axis()
    xa.min, xa.max = 0,25
    chart.x_axis = xa
    ya = y_axis()
    ya.min, ya.max = -10, 10
    chart.y_axis = ya
    return chart

def test_shape():
    months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    chart = open_flash_chart()
    t = title(text="Random shape")
    chart.title = t
    s = shape(colour="#89ad34")
    s.values = [{'x': random.randint(1,10), 'y': random.randint(1,10)} for i in range(5)]
    chart.add_element(s)
    x = x_axis()
    x.offset = False
    x.labels = x_axis_labels(labels=months)
    chart.x_axis = x
    return chart

def test_x_axis_labels_1():

    t = title(text='X Axis Labels')
    l = line_dot()
    l.values = list(range(9,0,-1))

    x = x_axis()
    x.colour = '#428C3E'
    x.grid_colour = '#86BF83'
    lbl = x_axis_labels(labels=['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'])
    x.labels = lbl
    chart = open_flash_chart()
    chart.title = t
    chart.add_element(l)
    chart.x_axis = x
    return chart

def test_x_axis_labels_3():

    t = title(text='X Axis Labels Complex Example')
    l = line_dot()
    l.values = list(range(9,0,-1))

    x = x_axis()
    x.stroke = 1
    x.colour = '#428C3E'
    x.tick_height = 5
    x.steps = 1
    x.grid_colour = '#86BF83'
    xlbls = x_axis_labels(steps=2, rotate=270, colour='#FF0000', size=16)

    lbls = ['one', 'two', 'three', 'four', 'five']
    lbls.append(x_axis_label(text='six', colour='#0000FF', size=30, rotate=270))
    lbls.append(x_axis_label(text='seven', colour='#0000FF', size=30, rotate=270))
    eight = x_axis_label(text='eight', colour='#8C773E', size=16, rotate=315)
    eight.visible = True
    lbls.append(eight)
    lbls.append(x_axis_label(text='nine', colour='#2683CF', size=16, rotate=0))
    xlbls.labels = lbls
    x.labels = xlbls
    chart = open_flash_chart()
    chart.title = t
    chart.add_element(l)
    chart.x_axis = x
    return chart

def make_chart(num, data):
    return '''
<div id='div_chart%(num)s'></div>
<script type="text/javascript">
function get_data%(num)s(){ return JSON.stringify(%(data)s); }
swfobject.embedSWF(
"data/open-flash-chart.swf", "div_chart%(num)s", "700", "300", "9.0.0",
"expressInstall.swf", { "get-data":"get_data%(num)s"}
);
</script>
''' % locals()

print('''
<html>
<head>
<script type="text/javascript" src="data/json2.js"></script>
<script type="text/javascript" src="data/swfobject.js"></script>
</head>
<body>
''')

print((make_chart(15, test_area_hollow())))
print((make_chart(16, test_area_line())))
print((make_chart(0, test_bar())))
print((make_chart(1, test_bar_2())))
print((make_chart(2, test_bar_filled())))
print((make_chart(3, test_horizontal_bar())))
print((make_chart(4, test_line())))
print((make_chart(5, test_line_dot())))
print((make_chart(6, test_line_hollow())))
print((make_chart(7, test_manylines())))
print((make_chart(8, test_radar_charts_1())))
print((make_chart(9, test_radar_charts_2())))
print((make_chart(10, test_radar_charts_3())))
print((make_chart(105, test_scatter_chart())))
print((make_chart(11, test_scatter_line_chart())))
print((make_chart(12, test_shape())))
print((make_chart(13, test_x_axis_labels_1())))
print((make_chart(14, test_x_axis_labels_3())))

print('''
</body>
</html>
''')
